﻿using QG.JobBack.BLL.IService;
using QG.JobBack.DAL;
using QH.JobBack.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace QG.JobBack.BLL.Service
{
    public class FunctionService : BaseRepository<Function>, IFunctionService
    {
        ResponseResult<Function> result = new ResponseResult<Function>();
        /// <summary>
        /// 添加一个功能
        /// </summary>
        /// <param name="function"></param>
        /// <returns></returns>
        public ResponseResult<Function> AddFunction(Function function)
        {
            result = new ResponseResult<Function>();
            function.CreateTime = DateTime.Now;
            function.UpdateTime = DateTime.Now;
            result.success = Add(function);
            result.msg = result.success ? "添加成功" : "添加失败";
            return result;
        }
        /// <summary>
        /// 修改一个功能
        /// </summary>
        /// <param name="function"></param>
        /// <returns></returns>
        public ResponseResult<Function> UpdateFunction(Function function)
        {
            result = new ResponseResult<Function>();
            //先查出对象
            var model = Query(a => a.Id == function.Id);
            //赋值
            model.NAME = function.NAME;
            model.UpdateTime = DateTime.Now;
            model.ActionName = function.ActionName;
            model.ControllerName = function.ControllerName;
            model.MenuId = function.MenuId;
            model.Remark = function.Remark;
            model.FunctionCode = function.FunctionCode;

            result.success = Update(model);
            result.msg = result.success ? "修改成功" : "修改失败";
            return result;
        }

        /// <summary>
        /// 根据Id删除一个功能
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseResult<Function> DeleteFunctionById(int id)
        {
            result = new ResponseResult<Function>();
            result.success = DeleteById(id);
            result.msg = result.success ? "删除成功" : "删除失败";
            return result;
        }

        /// <summary>
        /// 查询功能列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="functionName"></param>
        /// <returns></returns>
        public ResponseResult<Function> GetFunctionList(int page,int limit,string functionName)
        {
            result = new ResponseResult<Function>();
            //构造查询条件
            int total;
            Expression<Func<Function, bool>> whereLambda = a => true;
            //如果功能名称不能为空，过滤
            if (!string.IsNullOrEmpty(functionName))
            {
                whereLambda = a => a.NAME.Contains(functionName);
            }
            Expression<Func<Function, int>> orderLambda = a => a.Id;
            var list = QueryPageList(page,limit,whereLambda,orderLambda,out total,true);
            //对象赋值
            result.code = 0;
            result.count = total;
            result.msg = string.Empty;
            result.data = list;
            return result;
        }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseResult<Function> GetFunction(int id)
        {
            result = new ResponseResult<Function>();
            result.data = Query(a => a.Id == id);
            return result;
        }

        /// <summary>
        /// 获取某个菜单下的功能
        /// </summary>
        /// <returns></returns>
        public ResponseResult<Function> GetAllFunction()
        {
            result = new ResponseResult<Function>();
            using (var db=new qgpingEntities())
            {
                var linq = from a in db.Function
                           join b in db.Menuinfo
                           on a.MenuId equals b.MenuId
                           select a;
                result.data = linq.ToList();

                return result;
            }
        }

        /// <summary>
        /// 获取某个组下的功能
        /// </summary>
        /// <param name="moduleid"></param>
        /// <returns></returns>
        public ResponseResult<Function> GetModuleFunction(int moduleid)
        {
            ResponseResult<Function> result = new ResponseResult<Function>();
            using (var db=new qgpingEntities())
            {
                var linq = from a in db.Function
                           join b in db.Menuinfo
                           on a.MenuId equals b.MenuId
                           where b.ModuleId == moduleid
                           select a;
                result.data = linq.ToList();
                return result;
            }
        }
    }
}
